package cn.com.qingz.modules.system.service.impl;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import cn.com.qingz.core.constantes.SystemConstantes;
import cn.com.qingz.modules.system.mapper.SystemparameterMapper;
import cn.com.qingz.modules.system.model.Systemparameter;
import cn.com.qingz.modules.system.service.BasicService;

@Service
public class BasicServiceImpl implements BasicService {

    @Autowired
    private SystemparameterMapper mapper;

    /**
     * 系统初始化
     * @param application
     */
    @Override
    public void doInitApplication(ServletContext application) {
        List<Systemparameter> paramList = mapper.listAllParam();
        Map<String, String> sysparam = new HashMap<String, String>();
        for (Systemparameter param : paramList) {
            sysparam.put(param.getParcode(), param.getParvalue());
        }
        application.setAttribute(SystemConstantes.SYS_PARAM, sysparam);
    }

    /**
     * 上传文件
     * @param file
     * @return 文件id
     * @throws IOException
     */
    public String doSaveFile(MultipartFile file) throws IOException {
        //		MongoClient db = new MongoClient(file_db_ip, file_db_port);
        //		DB mydb = db.getDB(file_db_database);
        //		GridFS myFS = new GridFS(mydb, file_db_bucket);
        //
        //		GridFSInputFile inputFile = myFS.createFile(file.getBytes());
        //		String guid_filename = java.util.UUID.randomUUID().toString().replace("-", "");
        //		inputFile.setFilename(guid_filename);
        //
        //		BasicDBObject obj = new BasicDBObject();
        //		// obj.put("file_name", URLEncoder.encode(file.getOriginalFilename(), "UTF-8"));
        //		obj.put("file_name", file.getOriginalFilename());
        //		obj.put("ContentType", file.getContentType());
        //		obj.put("AttachParameters", "");
        //		inputFile.setMetaData(obj);
        //
        //		logger.error("上传文件：" + file.getOriginalFilename() + ";id=" + guid_filename);
        //
        //		inputFile.save();
        //		db.close();
        //		return guid_filename;
        return "";
    }

    /**
     * 上传图片文件
     * @param file
     * @return
     */
    public String doSaveFile(BufferedImage image) throws IOException {
        //		MongoClient db = new MongoClient(file_db_ip, file_db_port);
        //		DB mydb = db.getDB(file_db_database);
        //		GridFS myFS = new GridFS(mydb, file_db_bucket);
        //
        //		ByteArrayOutputStream out = new ByteArrayOutputStream();
        //		ImageIO.write(image, "png", out);
        //
        //		GridFSInputFile inputFile = myFS.createFile(out.toByteArray());
        //		String guid_filename = java.util.UUID.randomUUID().toString().replace("-", "");
        //		inputFile.setFilename(guid_filename);
        //
        //		BasicDBObject obj = new BasicDBObject();
        //		obj.put("file_name", URLEncoder.encode("Thumbnails.png", "UTF-8"));
        //		obj.put("ContentType", "image/png");
        //		obj.put("AttachParameters", "");
        //		inputFile.setMetaData(obj);
        //
        //		inputFile.save();
        //		db.close();
        //		return guid_filename;
        return "";
    }

    /**
     * 删除文件
     * @param filename
     * @return
     */
    public boolean doDelFile(String filename) throws IOException {
        //		MongoClient mongo = null;
        //		mongo = new MongoClient(file_db_ip, file_db_port); // 数据库地址，端口号
        //		DB myDB = mongo.getDB(file_db_database); // 数据库名称
        //		GridFS gridFS = new GridFS(myDB, file_db_bucket);
        //		gridFS.remove(filename);
        //		logger.error("删除文件：" + filename);
        //		mongo.close();
        return true;
    }

}
